package com.appleyk.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import com.appleyk.core.CoreProcessor;
import com.appleyk.repository.QuestionRepository;
import com.appleyk.service.QuestionService;


/**
 * <p>核心问答业务实现类</p>
 *
 * @author Appleyk
 * @version V.0.1.2
 * @blob https://blog.csdn.net/Appleyk
 * @date updated on 21:21 2020/3/31
 */
@Service
@Primary
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private CoreProcessor queryProcess;

    @Override
    public String answer(String question) throws Exception {

        List<String> reStrings = queryProcess.analysis(question);
        int modelIndex = Integer.valueOf(reStrings.get(0));
        String answer =null;
        String name;

        /**匹配问题模板*/
        switch (modelIndex) {
            case 0:
                answer = getUpIndustry(reStrings);
                break;
            case 1:
                answer = getDownIndustry(reStrings);
                break;
            case 2:
                answer = getDownCompany(reStrings);
                break;
            case 3:
                answer = getCompanyCode(reStrings);
                break;
            case 4:
                answer = getCompanyLocation(reStrings);
                break;
            case 5:
                answer = getCompanyTime(reStrings);
                break;
            case 6:
                answer = getCompanyFullName(reStrings);
                break;
            case 7:
                answer = getCompanyProduct(reStrings);
                break;
            case 8:
                answer = getCompanyIndustry(reStrings);
                break;
            case 9:
                answer = getCooperateCompany(reStrings);
                break;
            case 10:
                answer = getCompeteCompany(reStrings);
                break;
            case 11:
                answer = getProductSmall(reStrings);
                break;
            case 12:
                answer = getProductBig(reStrings);
                break;
            case 13:
                answer = getProductDown(reStrings);
                break;
            case 14:
                answer = getProductUp(reStrings);
                break;
            case 15:
                answer = getProductCompany(reStrings);
                break;
            case 16:
                answer = getCompanyRelation(reStrings);
                break;
            case 17:
                answer = getIndustryRelation(reStrings);
                break;
            case 18:
                answer = getProductRelation(reStrings);
                break;
            case 19:
                answer = getIndustryCompanyRelation(reStrings);
                break;
            case 20:
                answer = getCompanyProductRelation(reStrings);
                break;
            default:
                break;
        }
        System.out.println(answer);
        if (answer != null && !"".equals(answer) && !("\\N").equals(answer)) {
            return answer;
        } else {
            return "sorry,小主,我没有找到你要的答案";
        }
    }

    /**nm 上级行业 == in 上级行业*/
    private String getUpIndustry(List<String> reStrings) {
        String industryName;
        String answer;
        industryName = reStrings.get(1);
        String uoIndustry = questionRepository.getUpIndustry(industryName);
        if (uoIndustry == null) {
            answer = null;
        } else {
            answer = uoIndustry;
        }
        return answer;
    }

    /** 下级行业 == in 下级行业*/
    private String getDownIndustry(List<String> reStrings) {
        String industryName;
        String answer;
        industryName= reStrings.get(1);
        List<String> downIndustry = questionRepository.getDownIndustry(industryName);
        if (downIndustry.size() == 0) {
            answer = null;
        } else {
            answer = downIndustry.toString().replace("[", "").replace("]", "");
        }
        return answer;
    }

    /**nm 包含公司 == co 所属行业*/
    private String getDownCompany(List<String> reStrings) {
        String industryName;
        String answer;
        industryName = reStrings.get(1);
        List<String> companys = questionRepository.getDownCompany(industryName);
        if (companys.size() == 0) {
            answer = null;
        } else {
            answer = companys.toString().replace("[", "").replace("]", "");
        }
        return answer;
    }

    /** co 公司代码 == 公司股票代码*/
    private String getCompanyCode(List<String> reStrings) {
        String companyName;
        String answer;
        companyName = reStrings.get(1);
        String code = questionRepository.getCompanyCode(companyName);
        if (code == null) {
            answer = null;
        } else {
            answer = code;
        }
        return answer;
    }

    /** co 上市地点 == 公司上市地点*/
    private String getCompanyLocation(List<String> reStrings) {
        String companyName;
        String answer;
        companyName = reStrings.get(1);
        String location = questionRepository.getCompanyLocation(companyName);
        if (location == null) {
            answer = null;
        } else {
            answer = location;
        }
        return answer;
    }

    /** co 上市时间 == 公司的上市时间*/
    private String getCompanyTime(List<String> reStrings) {
        String companyName;
        String answer;
        companyName = reStrings.get(1);
        String time = questionRepository.getCompanyTime(companyName);
        if (time == null) {
            answer = null;
        } else {
            answer = time;
        }
        return answer;
    }

    /** co 全称 == 公司全称*/
    private String getCompanyFullName(List<String> reStrings) {
        String companyName;
        String answer;
        companyName = reStrings.get(1);
        String fullName = questionRepository.getCompanyFullName(companyName);
        if (fullName == null) {
            answer = null;
        } else {
            answer = fullName;
        }
        return answer;
    }

    /** co 主营产品 == 公司的主营产品 */
    private String getCompanyProduct(List<String> reStrings) {
        String companyName;
        String answer;
        companyName = reStrings.get(1);
        List<String> products = questionRepository.getCompanyProduct(companyName);
        if (products.size() == 0) {
            answer = null;
        } else {
            answer = products.toString().replace("[", "").replace("]", "");
        }
        return answer;
    }

    /**= co 所属行业 == 公司的所属行业*/
    private String getCompanyIndustry(List<String> reStrings) {
        String companyName;
        String answer;
        companyName = reStrings.get(1);
        String industry = questionRepository.getCompanyIndustry(companyName);
        if (industry == null) {
            answer = null;
        } else {
            answer = industry;
        }
        return answer;
    }

    /** co 合作伙伴 == 公司合作伙伴*/
    private String getCooperateCompany(List<String> reStrings) {
        String companyName;
        String answer;
        companyName = reStrings.get(1);
        List<String> mates = questionRepository.getCooperateCompany(companyName);
        if (mates.size() == 0) {
            answer = null;
        } else {
            answer = mates.toString().replace("[", "").replace("]", "");
        }
        return answer;
    }

    /** co 竞争对手 == 公司竞争对手*/
    private String getCompeteCompany(List<String> reStrings) {
        String companyName;
        String answer;
        companyName = reStrings.get(1);
        List<String> opponents = questionRepository.getCompeteCompany(companyName);
        if (opponents.size() == 0) {
            answer = null;
        } else {
            answer = opponents.toString().replace("[", "").replace("]", "");
        }
        return answer;
    }

    /** pr 产品小类 == 某个产品的小类*/
    private String getProductSmall(List<String> reStrings) {
        String productName;
        String answer;
        productName = reStrings.get(1);
        List<String> productSmall = questionRepository.getProductSmall(productName);
        if (productSmall.size() == 0) {
            answer = null;
        } else {
            answer = productSmall.toString().replace("[", "").replace("]", "");
        }
        return answer;
    }

    /** pr 产品大类 == 某个产品的大类*/
    private String getProductBig(List<String> reStrings) {
        String productName;
        String answer;
        productName = reStrings.get(1);
        List<String> productBig = questionRepository.getProductBig(productName);
        if (productBig.size() == 0) {
            answer = null;
        } else {
            answer = productBig.toString().replace("[", "").replace("]", "");
        }
        return answer;
    }

    /** pr 下游产品 == 某个产品的下游产品*/
    private String getProductDown(List<String> reStrings) {
        String productName;
        String answer;
        productName = reStrings.get(1);
        List<String> productDown = questionRepository.getProductDown(productName);
        if (productDown.size() == 0) {
            answer = null;
        } else {
            answer = productDown.toString().replace("[", "").replace("]", "");
        }
        return answer;
    }

    /** pr 上游材料 == 某个产品的上游材料*/
    private String getProductUp(List<String> reStrings) {
        String productName;
        String answer;
        productName = reStrings.get(1);
        List<String> productUp = questionRepository.getProductUp(productName);
        if (productUp.size() == 0) {
            answer = null;
        } else {
            answer = productUp.toString().replace("[", "").replace("]", "");
        }
        return answer;
    }

    /** pr 所属公司 == 某个产品的所属公司*/
    private String getProductCompany(List<String> reStrings) {
        String productName;
        String answer;
        productName = reStrings.get(1);
        List<String> productCompany = questionRepository.getProductCompany(productName);
        if (productCompany.size() == 0) {
            answer = null;
        } else {
            answer = productCompany.toString().replace("[", "").replace("]", "");
        }
        return answer;
    }

    /**= x 和 y 关系 == 两个公司的关系 */
    private String getCompanyRelation(List<String> reStrings) {
        String name1,name2;
        String answer;
        name1 = reStrings.get(1);
        name2 = reStrings.get(2);
        List<String> industry = questionRepository.getCompanyRelation1(name1,name2);
        List<String> product = questionRepository.getCompanyRelation2(name1,name2);
        if (industry.size() != 0 && product.size() != 0) {
            answer = "两公司属于同一个行业:"+industry.toString().replace("[", "").replace("]", "")
                    +"\n两公司有相同的主营产品"+product.toString().replace("[", "").replace("]", "");
        }else if(product.size() == 0){
            answer = "两公司属于同一个行业:"+industry.toString().replace("[", "").replace("]", "");
        }else {
            answer = "两公司有相同的主营产品"+product.toString().replace("[", "").replace("]", "");
        }
        return answer;
    }

    /**= x 和 y 关系 == 行业与行业的关系 */
    private String getIndustryRelation(List<String> reStrings) {
        String name1,name2;
        String answer;
        name1 = reStrings.get(1);
        name2 = reStrings.get(2);
        System.out.println(name1);
        System.out.println(name2);
        String type1 = questionRepository.getIndustryRelation1(name1,name2);
        String type2 = questionRepository.getIndustryRelation2(name1,name2);
        String industry = questionRepository.getIndustryRelation3(name1,name2);
        if (type1 != null) {
            answer = name1+"的上级行业是"+name2;
        }else if(type2 != null){
            answer = name2+"的上级行业是"+name1;
        }else if(industry != null){
            answer = name1+"和"+name2+"有共同的上级行业："+industry;
        } else{
            answer = null;
        }
        return answer;
    }

    /**= x 和 y 关系 == 产品与产品的关系 */
    private String getProductRelation(List<String> reStrings) {
        String name1,name2;
        String answer=null;
        name1 = reStrings.get(1);
        name2 = reStrings.get(2);
        System.out.println(name1);
        System.out.println(name2);
        String type1 = questionRepository.getProductRelation1(name1,name2);
        String type2 = questionRepository.getProductRelation2(name1,name2);
        String type3 = questionRepository.getProductRelation3(name1,name2);
        String type4 = questionRepository.getProductRelation4(name1,name2);
        String product1 = questionRepository.getProductRelation5(name1,name2);
        String product2 = questionRepository.getProductRelation6(name1,name2);
        String type5 = questionRepository.getProductRelation7(name1,name2);
        String type6 = questionRepository.getProductRelation8(name1,name2);
        String comany = questionRepository.getProductRelation9(name1,name2);
        if (type1 != null && type4 != null)
            answer = name2+"的下游产品是"+name1+","+name1+"的上游材料是"+name2;
        else if(type2 != null && type3 != null)
            answer = name1+"的下游产品是"+name2+","+name2+"的上游材料是"+name1;
        else if(type1 != null)
            answer = name1+"的上游材料是"+name2;
        else if(type2 != null)
            answer = name2+"的上游材料是"+name1;
        else if(type3 != null)
            answer = name1+"的下游产品是"+name2;
        else if(type4 != null)
            answer = name2+"的下游产品是"+name1;
        else if(product1 != null)
            answer = name1+"和"+name2+"有共同的上游材料"+product1;
        else if(product2 != null)
            answer = name1+"和"+name2+"有共同的产品大类"+product2;
        else if(type5 != null)
            answer = name1+"的产品小类是"+name2;
        else if(type6 != null)
            answer = name2+"的产品小类是"+name1;
        else if(comany != null)
            answer = name1+"和"+name2+"用相同的经营公司"+comany;
        return answer;
    }

    /**= x 和 y 关系 == 行业与公司的关系 */
    private String getIndustryCompanyRelation(List<String> reStrings) {
        String name1,name2;
        String answer;
        name1 = reStrings.get(1);
        name2 = reStrings.get(2);
//        System.out.println(name1);
//        System.out.println(name2);
        String industry1 = questionRepository.getIndustryCompanyRelation1(name1,name2);
//        System.out.println(industry1);
        String industry2 = questionRepository.getIndustryCompanyRelation2(name1,name2);
//        System.out.println(industry2);
        if (industry1 != null) {
            answer = name2+"公司属于"+name1+"行业";
//            System.out.println(industry1);
        }else if(industry2 != null){
            answer = name2+"公司所属行业为:"+industry2+"，并且且该行业是"+name1+"的下级行业";
//            System.out.println(industry2);
        }else{
            answer = null;
        }
        return answer;
    }

    /**= x 和 y 关系 == 公司与产品的关系 */
    private String getCompanyProductRelation(List<String> reStrings) {
        String name1, name2;
        String answer;
        name1 = reStrings.get(1);
        name2 = reStrings.get(2);
        System.out.println(name1);
        System.out.println(name2);
        List<String> type = questionRepository.getCompanyProductRelation1(name1,name2);
        List<String> product1 = questionRepository.getCompanyProductRelation2(name1, name2);
        List<String> product2 = questionRepository.getCompanyProductRelation3(name1, name2);
        List<String> product3 = questionRepository.getCompanyProductRelation4(name1, name2);
        List<String> product4 = questionRepository.getCompanyProductRelation5(name1, name2);
        System.out.println(type+"\n"+product1+"\n"+product2+"\n"+product3+"\n"+product4+"\n");
        if(product1.size() != 0 ){
            answer = name1+" 公司的主营产品有:"+product1.toString().replace("[", "").replace("]", "")+"，并且该产品的产品小类包含 "+name2+" 产品";
        }else if(product2.size() != 0){
            answer = name1+" 公司的主营产品有:"+product2.toString().replace("[", "").replace("]", "")+"，并且该产品是 "+name2+" 产品的产品小类";
        }else if(product3.size() != 0){
            answer = name1+" 公司的主营产品有:"+product3.toString().replace("[", "").replace("]", "")+"，并且该产品的上游材料包含 "+name2+" 产品";
        }else if(product4.size() != 0){
            answer = name1+" 公司的主营产品有:"+product4.toString().replace("[", "").replace("]", "")+"，并且该产品是 "+name2+" 产品的下游产品";
        }else if (type.size() != 0) {
            answer = name1+" 公司的主营产品包含 "+name2+" 产品";
        }else{
            answer = null;
        }
        return answer;
    }
}
